﻿using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace AiXiu.DAL
{
    public class UserDAL
    {
        //实例化EF
        AiXiuDbContext dbContext = new AiXiuDbContext();
        # region 查询用户列表数据(UserList页面测试)
        public List<TBUsers> GetList()
        {
            //查询用户列表数据
            return dbContext.TBUsers.ToList();
        }
        #endregion
        /// <summary>
        /// 查找用户的具体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TBUsers getUserInfo(int id)
        {
            return dbContext.TBUsers.Find(id);
        }
        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public TBUsers UpdateUserInfo(TBUsers users)
        {
            TBUsers userInfo = dbContext.TBUsers.SingleOrDefault(u => u.Id == users.Id);
            if (userInfo == null)
            {
                return null;
            }
            userInfo.NickName = users.NickName;
            userInfo.Birthday = users.Birthday;
            userInfo.Sex = users.Sex;
            userInfo.Address = users.Address;
            userInfo.Hobby = users.Hobby;
            //更新到数据库
            dbContext.Entry(userInfo).State = EntityState.Modified;
            dbContext.SaveChanges();
            //返回结果
            return userInfo;
        }
        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <param name="users2"></param>
        /// <returns></returns>
        public TBUsers UpdateHeadPortrait(TBUsers users)
        {
            TBUsers userInfo = dbContext.TBUsers.SingleOrDefault(u => u.Id == users.Id);
            if (userInfo == null)
            {
                return null;
            }
            userInfo.Avatar = users.Avatar;
            //更新到数据库
            dbContext.Entry(userInfo).State = EntityState.Modified;
            dbContext.SaveChanges();
            //返回结果
            return userInfo;
        }
    }
}
